<template>
  <a-drawer
    :title="title"
    :width="drawerWidth"
    @close="handleCancel"
    :visible="visible"
    :confirmLoading="confirmLoading">

    <div :style="{width: '100%',border: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',}">
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="form" :model="model" :rules="validatorRules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :label="menuLabel"
            prop="title"
            hasFeedback >
            <a-input placeholder="请输入菜单名称" v-model="model.title" :readOnly="disableSubmit"/>
          </a-form-model-item>

          <a-form-model-item
            label="上级菜单"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :validate-status="validateStatus"
            :hasFeedback="true"
            :required="true">
            <span slot="help">{{ validateStatus=='error'?'请选择上级菜单':'&nbsp;&nbsp;' }}</span>
            <a-tree-select
              style="width:100%"
              :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
              :treeData="treeData"
              v-model="model.parentId"
              placeholder="请选择父级菜单"
              :disabled="disableSubmit"
              :replace-fields="replaceFields"
              @change="handleParentIdChange">
            </a-tree-select>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="path"
            label="菜单路径">
            <a-input placeholder="请输入菜单路径" v-model="model.path" :readOnly="disableSubmit"/>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="component"
            label="前端组件">
            <a-input placeholder="请输入前端组件" v-model="model.component" :readOnly="disableSubmit"/>
          </a-form-model-item>

          <a-form-model-item
            v-show="model.children!=null"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="默认跳转地址">
            <a-input placeholder="请输入路由参数 redirect" v-model="model.redirect" :readOnly="disableSubmit"/>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="菜单图标">
            <a-input placeholder="点击选择图标" v-model="model.icon" :readOnly="disableSubmit">
              <a-icon slot="addonAfter" type="setting" @click="selectIcons" />
            </a-input>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="sortNum"
            label="排序">
            <a-input-number placeholder="请输入菜单排序" v-model="model.sortNum" style="width: 200px" :readOnly="disableSubmit"/>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="是否隐藏">
            <a-switch checkedChildren="是" unCheckedChildren="否" v-model="model.hidden"/>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="是否缓存">
            <a-switch checkedChildren="是" unCheckedChildren="否" v-model="model.keepAlive"/>
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="是否外部链接">
            <a-switch checkedChildren="是" unCheckedChildren="否" v-model="model.externalLink"/>
          </a-form-model-item>

        </a-form-model>

        <!-- 选择图标 -->
        <icons @choose="handleIconChoose" @close="handleIconCancel" :iconChooseVisible="iconChooseVisible"></icons>
      </a-spin>
      <a-row :style="{textAlign:'right'}">
        <a-button :style="{marginRight: '8px'}" @click="handleCancel">
          关闭
        </a-button>
        <a-button :disabled="disableSubmit" @click="handleOk" type="primary">确定</a-button>
      </a-row>
    </div>
  </a-drawer>
</template>

<script>
  import { saveOrUpdate, queryUserMenuTree } from '@/api/system'
  import Icons from './icon/Icons'

  export default {
    name: 'MenuModal',
    components: { Icons },
    data () {
      return {
        drawerWidth: 700,
        replaceFields: {
          key: 'id',
          value: 'id'
        },
        treeData: [],
        title: '操作',
        visible: false,
        disableSubmit: false,
        model: {},
        show: true, // 根据菜单类型，动态显示隐藏表单元素
        menuLabel: '菜单名称',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        iconChooseVisible: false,
        validateStatus: ''
      }
    },
    computed: {
      validatorRules: function () {
        return {
          title: [{ required: true, message: '请输入菜单标题!' }],
          component: [{ required: true, message: '请输入前端组件!' }],
          path: [{ required: true, message: '请输入菜单路径!' }]
        }
      }
    },
    created () {
    },
    methods: {
      loadTree () {
        var that = this
        queryUserMenuTree().then((res) => {
          if (res.success) {
            that.treeData = res.data
          }
        })
      },
      add () {
        // 初始化默认值
        this.edit({ status: '1', permsType: '1', sortNo: 1.0, route: true, menuType: 0 })
      },
      edit (record) {
        this.resetScreenSize() // 调用此方法,根据屏幕宽度自适应调整抽屉的宽度
        this.model = Object.assign({}, record)
        this.show = record.children === null
        this.visible = true
        this.loadTree()
      },
      close () {
        this.$emit('close')
        this.disableSubmit = false
        this.visible = false
        this.$refs.form.resetFields()
      },
      handleOk () {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            const obj = saveOrUpdate(that.model)
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          } else {
            return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateNumber (rule, value, callback) {
        if (!value || new RegExp(/^[0-9]*[1-9][0-9]*$/).test(value)) {
          callback()
        } else {
          callback(new Error('请输入正整数!'))
        }
      },
      selectIcons () {
        this.iconChooseVisible = true
      },
      handleIconCancel () {
        this.iconChooseVisible = false
      },
      handleIconChoose (value) {
        console.log(value)
        this.model.icon = value
        this.iconChooseVisible = false
      },
      // 根据屏幕变化,设置抽屉尺寸
      resetScreenSize () {
        const screenWidth = document.body.clientWidth
        if (screenWidth < 500) {
          this.drawerWidth = screenWidth
        } else {
          this.drawerWidth = 700
        }
      },
      handleParentIdChange (value) {
        if (!value) {
          this.validateStatus = 'error'
        } else {
          this.validateStatus = 'success'
        }
      }
    }
  }
</script>

<style scoped>

</style>
